/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alxbrendmaquinarias.model;

import com.alxbrendmaquinarias.util.VarUtil;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

/**
 *
 * @author Max
 */
@Entity
@Table(name = "sale_product", catalog = "sgcom", schema = "")
@NamedQueries({
    @NamedQuery(name = "SaleProduct.findAll", query = "SELECT s FROM SaleProduct s"),
    @NamedQuery(name = "SaleProduct.findById", query = "SELECT s FROM SaleProduct s WHERE s.id = :id"),
    @NamedQuery(name = "SaleProduct.findByDateSale", query = "SELECT s FROM SaleProduct s WHERE s.dateSale = :dateSale"),
    @NamedQuery(name = "SaleProduct.findByTypeMoney", query = "SELECT s FROM SaleProduct s WHERE s.typeMoney = :typeMoney"),
    @NamedQuery(name = "SaleProduct.findByNumberDocument", query = "SELECT s FROM SaleProduct s WHERE s.numberDocument = :numberDocument"),
    @NamedQuery(name = "SaleProduct.findByDescription", query = "SELECT s FROM SaleProduct s WHERE s.description = :description")})
public class SaleProduct implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "date_sale")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateSale;
    @Size(max = 45)
    @Column(name = "type_money")
    private String typeMoney;
    @Size(max = 10)
    @Column(name = "number_document")
    private String numberDocument;
    @Size(max = 200)
    @Column(name = "description")
    private String description;
    @JoinColumn(name = "employee_id", referencedColumnName = "id")
    @ManyToOne
    private Employee employeeId;
    @JoinColumn(name = "client_id", referencedColumnName = "id")
    @ManyToOne
    private Client clientId;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "saleProductId")
    private List<DetailSaleProduct> detailSaleProductList;
    @Column(name = "total_amount")
    private Double totalAmount;
    @Column(name = "exchange_rate")
    private Double exchangeRate;
    @Column(name = "sub_total")
    private Double subTotal;
    @Column(name = "igv")
    private Double igv;
    @Column(name = "document_type")
    private String documentType;
    

    public SaleProduct() {
    }
    
    public void CalculateTotalAmount()
    {
        if(detailSaleProductList!=null)
        {
            for(DetailSaleProduct dsp: detailSaleProductList)
            {
                subTotal += dsp.getAmount()*dsp.getPriceUnit(); 
            }
        }
        totalAmount = (igv/100.0)*getSubTotal()+getSubTotal();
    }

    public SaleProduct(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getDateSale() {
        return dateSale;
    }

    public void setDateSale(Date dateSale) {
        this.dateSale = dateSale;
    }

    public String getTypeMoney() {
        return typeMoney;
    }

    public void setTypeMoney(String typeMoney) {
        this.typeMoney = typeMoney;
    }

    public String getNumberDocument() {
        return numberDocument;
    }

    public void setNumberDocument(String numberDocument) {
        this.numberDocument = numberDocument;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Employee getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Employee employeeId) {
        this.employeeId = employeeId;
    }

    public Client getClientId() {
        return clientId;
    }

    public void setClientId(Client clientId) {
        this.clientId = clientId;
    }

    
    public List<DetailSaleProduct> getDetailSaleProductList() {
        return detailSaleProductList;
    }

    public void setDetailSaleProductList(List<DetailSaleProduct> detailSaleProductList) {
        this.detailSaleProductList = detailSaleProductList;
    }
    
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SaleProduct)) {
            return false;
        }
        SaleProduct other = (SaleProduct) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.alxbrendmaquinarias.model.SaleProduct[ id=" + id + " ]";
    }

    public Double getTotalAmount() {
        return VarUtil.roundDouble(totalAmount,2);
    }

    public void setTotalAmount(Double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public Double getExchangeRate() {
        return exchangeRate;
    }

    public void setExchangeRate(Double exchangeRate) {
        this.exchangeRate = exchangeRate;
    }

    public Double getSubTotal() {
        return VarUtil.roundDouble(subTotal,2);
    }

    public void setSubTotal(Double subTotal) {
        this.subTotal = subTotal;
    }

    public Double getIgv() {
        return igv;
    }

    public void setIgv(Double igv) {
        this.igv = igv;
    }

    public String getDocumentType() {
        return documentType;
    }

    public void setDocumentType(String documentType) {
        this.documentType = documentType;
    }
    
    
    
}
